﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Data;
using System.Security.Permissions;

namespace Bussiness {
    [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
    public class PartnerBuss {
        /// <summary>
        /// check partner
        /// </summary>
        /// <param name="partnerID">partner login name</param>
        /// <param name="partnerPwd">partner pw</param>
        /// <returns>partner id</returns>
        [SecurityPermission(SecurityAction.Demand, ControlDomainPolicy = true)]
        public int isPartner(String partnerLoginName, String partnerPwd) {
            try {
                PartnerRepository repository = new PartnerRepository();
                //md5 pw
                Helper helper = new Helper();
                partnerPwd = helper.Encrypt(partnerPwd);

                DataTable table = repository.isPartner(partnerLoginName, partnerPwd);

                if (table.Rows.Count == 1) {
                    return int.Parse(table.Rows[0][0].ToString());
                }
                else {
                    return -1;
                }
            }
            catch (Exception ex) {
                return -1;
            }
        }

        /// <summary>
        /// write log if login successful
        /// </summary>
        /// <param name="SID">session id</param>
        /// <param name="partnerID">partner login name</param>
        /// <returns>true / false</returns>
        public bool writeLogLogin(String SID, int partnerID) {
            LoginLogRepository repository = new LoginLogRepository();
            if (repository.writeLog(SID, partnerID))
                return true;
            
            return false;
        }

        /// <summary>
        /// get partner list
        /// </summary>
        /// <returns></returns>
        public DataTable getPartnerList() {
            PartnerRepository repository = new PartnerRepository();
            return repository.getPartnerList();
        }

        public DataTable getPartner(int partnerID) {
            PartnerRepository repository = new PartnerRepository();
            return repository.getPartner(partnerID);
        }

        /// <summary>
        /// update partners
        /// </summary>
        /// <param name="partnerID"></param>
        /// <param name="partnerName"></param>
        /// <param name="partnerLogin"></param>
        /// <param name="partnerPwd"></param>
        /// <param name="wsName"></param>
        /// <param name="wsLink"></param>
        /// <param name="pvdID"></param>
        /// <param name="pvdPwd"></param>
        /// <returns></returns>
        public bool update(int partnerID, String partnerName, String partnerLogin, String partnerPwd, String wsName, String wsLink, String pvdID, String pvdPwd) {
            Helper hp = new Helper();
            PartnerRepository repository = new PartnerRepository();
            return repository.update(partnerID, partnerName, partnerLogin, hp.Encrypt(partnerPwd), wsName, wsLink, pvdID, pvdPwd);
        }

        public bool insert(String partnerName, String partnerLogin, String partnerPwd, String wsName, String wsLink, String pvdID, String pvdPwd) {
            Helper hp = new Helper();
            PartnerRepository repository = new PartnerRepository();
            return repository.insert(partnerName, partnerLogin, hp.Encrypt(partnerPwd), wsName, wsLink, pvdID, pvdPwd);
        }
    }
}
